package com.weiqiao.report.distributior.Service.impl;

import com.google.gson.Gson;
import com.weiqiao.report.distributior.Service.IDistributionReportService;
import com.weiqiao.report.distributior.domain.dto.UserReportEntityDTO;
import com.weiqiao.report.distributior.domain.vo.UserReportVO;
import com.weiqiao.report.distributior.repository.IDistributionRepository;
import com.weiqiao.utils.SecurityUtils;
import org.apache.commons.lang.StringUtils;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

@Service("ReportDistributionServiceImpl")
public class DistributionServiceImpl implements IDistributionReportService {

    @Autowired
    private IDistributionRepository distributionRepository;

    @Override
    public List<UserReportEntityDTO> getDistributionList(UserReportVO vo) {
        Integer company_id = vo.getCompany_id();
        Document doc = new Document(); // 查询条件
        doc.append("delete_flag", 0);
        if (!StringUtils.isBlank(vo.getKeywords())) {  // 手机号 微信昵称 姓名
            List<Document> List = new ArrayList<>();
            List.add(new Document("phone", new Document("$regex", vo.getKeywords())));
            List.add(new Document("nickname", new Document("$regex", vo.getKeywords())));
            List.add(new Document("username", new Document("$regex", vo.getKeywords())));
            doc.append("$or", List);
        }
        // 排序条件
        Document sorts = new Document("create_date", -1);
        doc.append("member_type", vo.getMember_type()); //等级 0:客户  1：分销员  2：团长  3：省长
        // 分页条件
        int skip = vo.getPage() * vo.getSize();
        List<String> list = distributionRepository.getdistributionList(doc, sorts, skip, vo.getSize(), company_id);
        List<UserReportEntityDTO> result = new ArrayList<>();
        Gson gson = new Gson();
        return result;
    }

    @Override
    public int countNum(UserReportVO vo) {
        Integer company_id = SecurityUtils.getCompanyId();
        Document doc = new Document();
        doc.append("delete_flag", 0);
        return distributionRepository.countFruiter(company_id, doc);
    }
}
